﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.General;
using DriveBeamEditor.Enums;
using DriveBeamEditor.Tools;
using JBeamLibrary;
using JBeamLibrary.Tools;

namespace DriveBeamEditor.Controls.BeamNG.Controls
{
    public partial class PreferencesControl : UserControl
    {
        #region CTORs

        public PreferencesControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!DesignMode)
                LoadConfig();
        }

        private void kvcGamePath_EditValueChanged(object sender, EventArgs e)
        {
            btGamePath.InitialDirectory = kvcGamePath.Value;
        }

        private void xbResetUserName_Click(object sender, EventArgs e)
        {
            Config.InitUserName();
            kvcDefaultUserName.Value = Config.Map[kvcDefaultUserName.Key];
        }

        private void kvcApplicationLayout_EditValueChanged(object sender, EventArgs e)
        {
            var mainForm = Application.OpenForms["MainForm"] as BeamNGMainForm;
            if (mainForm == null) return;

            mainForm.SetupLayout((ApplicationLayout)kvcApplicationLayout.EditValue);
        }

        #endregion

        #region Methods

        public void LoadConfig()
        {
            /* Assign values to controls */
            foreach (DictionaryEntry entry in Config.Map)
            {
                /* Game info controls */
                var gameCtrl = boxGameInfo.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals((string)entry.Key));
                if (gameCtrl != null)
                {
                    gameCtrl.Value = (string)entry.Value;
                    continue;
                }

                /* App info controls */
                var appCtrl = boxAppConfig.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals((string)entry.Key));
                if (appCtrl != null)
                {
                    appCtrl.Value = (string)entry.Value;
                    continue;
                }
            }
        }

        public void SaveConfig()
        {
            /* Update config */
            for (var i = 0; i < Config.Map.Count; i++)
            {
                /* Get current element */
                var entry = Config.Map[i];

                /* Game info controls */
                var gameCtrl = boxGameInfo.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals(entry.Key));
                if (gameCtrl != null)
                {
                    Config.Map.Set(entry.Key, gameCtrl.Value);
                    continue;
                }

                /* App info controls */
                var appCtrl = boxAppConfig.Controls.OfType<KeyValueControl>().FirstOrDefault(ctrl => ctrl.Key.Equals(entry.Key));
                if (appCtrl != null)
                {
                    Config.Map.Set(entry.Key, UIHelper.ValueToString(appCtrl.EditValue));
                    continue;
                }
            }

            /* Save config file */
            Config.Save();
        }

        #endregion
    }
}
